package com.atguigu.tingshu.order.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @author: hy
 * @date:2025/9/15
 * @description:
 */
@Slf4j
@Component
public class OrderReceiver {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 订单取消
     * @param orderNo
     * @param message
     * @param channel
     */
    @RabbitListener(queues = MqConst.QUEUE_CANCEL_ORDER)
    public void cancelOrder(String orderNo, Message message, Channel channel) {
        log.info("订单取消成功，订单编号：{}", orderNo);
        try {
            //判断
            if (!StringUtils.isEmpty(orderNo)){
                //取消订单
                orderInfoService.cancelOrder(orderNo);
            }
        } catch (Exception e) {
            //重试+落库
            log.error(e.getMessage(),e);
        }
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
